﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using TutorCenter.DataAccess.DataContext;
using TutorCenter.DataAccess.Entity;

namespace TutorCenter.DataAccess.Repository
{
    public class GenericRepository<T>: IGenericRepository<T> where T : Base
    {
        private readonly DbContext dbContext;
        private readonly DbSet<T> dbSet;
        private readonly IUnitOfWork unitOfWork;

        public GenericRepository(IUnitOfWork unitOfWork)
        {
            this.dbSet = unitOfWork.DbContext().Set<T>();
            this.dbContext = unitOfWork.DbContext();
            this.unitOfWork = unitOfWork;
        }
        public IQueryable<T> GetAll()
        {
            return dbSet;
        }

        public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate)
        {
            return dbSet.Where(predicate);
        }

        public void Add(T entity)
        {
            entity.IsDelete = false;
            this.dbSet.Add(entity);
            
        }

        public void Edit(T entity)
        {
            this.dbContext.Entry(entity).State = EntityState.Modified;
        }

        public void Delete(T entity)
        {
            this.dbSet.Remove(entity);
        }
    }
}
